﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClientDES
{
    public partial class Client : Form
    {
        string adUser = "";
        string adPass = "";
        public delegate void OnRecievedMessage(string message);
        public Login m_parentForm;
        public string Host;
        public string Nickname;
        private tcpConnection mChildConnection;
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        static extern uint ScrollText(System.IntPtr hwnd, uint wMsg, uint wParam, uint lParam);
        const int WM_VSCROLL = 0x115;
        const int SB_BOTTOM = 7;
        public Client(Login loginForm)
        {
            InitializeComponent();
            this.AcceptButton = (btnSend);
            m_parentForm = loginForm;
            this.txtMsg.KeyDown += new KeyEventHandler(txtMsg_KeyDown);
            this.FormClosing += new FormClosingEventHandler(Client_FormClosing);
            this.txtLog.TextChanged += new EventHandler(txtLog_TextChanged);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (txtMsg.Text != "")
            {
                if (txtMsg.Text.ToLower().StartsWith("/"))
                {
                    onCommand(txtMsg.Text);
                    txtMsg.Text = "";
                }
                else
                {
                    mChildConnection.SendMessage("MSG :" + txtMsg.Text);
                    txtMsg.Text = "";
                }
            }
        }

        private void txtMsg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                if (txtMsg.Text != "")
                {
                    if (txtMsg.Text.ToLower().StartsWith("/"))
                    {
                        onCommand(txtMsg.Text);
                        txtMsg.Text = "";
                    }
                    else
                    {
                        mChildConnection.SendMessage("MSG :" + txtMsg.Text);
                        txtMsg.Text = "";
                    }
                }
            }
        }

        private void onCommand(string message)
        {
            List<string> splitArray = new List<string>(message.Split(new char[] { '/' }));
            splitArray.RemoveAt(0);
            string midSplit = String.Join("/", splitArray.ToArray());
            splitArray = new List<string>(midSplit.Split(new char[] { ' ' }));

            string command = splitArray[0].ToUpper();
            splitArray.RemoveAt(0);
            List<string> args = splitArray;

            if (command == "KILL")
            {
                string userToKill = args[0];
                args.RemoveAt(0);
                mChildConnection.SendMessage("ADMIN " + adUser + " " + adPass + " :KILL " + userToKill + " " + String.Join(" ", args.ToArray()));
            }
            else if (command == "MUTE")
            {
                string userToKill = args[0];
                args.RemoveAt(0);
                mChildConnection.SendMessage("ADMIN " + adUser + " " + adPass + " :MUTE " + userToKill);
            }
            else if (command == "UNMUTE")
            {
                string userToKill = args[0];
                args.RemoveAt(0);
                mChildConnection.SendMessage("ADMIN " + adUser + " " + adPass + " :UNMUTE " + userToKill);
            }
            else if (command == "ME")
            {
                mChildConnection.SendMessage("ACTION :" + String.Join(" ", args.ToArray()));
            }
            else if (command == "NOTICE")
            {
                mChildConnection.SendMessage("NOTICE :" + String.Join(" ", args.ToArray()));
            }
            else if (command == "LOGIN")
            {
                adUser = args[0];
                adPass = args[1];
                mChildConnection.SendMessage("ADMIN " + adUser + " " + adPass + " :");
            }
        }

        public void StartConnection()
        {
            mChildConnection = new tcpConnection(this, Host, Nickname);
            mChildConnection.Connect();
        }
        public void Disconnected(string reason)
        {
            m_parentForm.Disconnected(reason);
        }
        public void RecievedMessage(string message)
        {
            if (InvokeRequired)
            {
                this.Invoke(new OnRecievedMessage(RecievedMessage), message);
            }
            else
            {
                UpdateTextbox(message);
            }
        }
        private void UpdateTextbox(string text)
        {
            txtLog.Text += "\r\n";
            txtLog.Text += text;
        }
        public delegate void Visability(bool visable);
        public void Vis(bool visable)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Visability(Vis), visable);
            }
            else
            {
                this.Visible = visable;
            }
        }

        private void Client_FormClosing(object sender, FormClosingEventArgs e)
        {
            mChildConnection.CloseConnection();
            m_parentForm.Close();
        }

        private void txtLog_TextChanged(object sender, EventArgs e)
        {
            ScrollText(txtLog.Handle, WM_VSCROLL, SB_BOTTOM, 0);
        }

        private void Client_Load(object sender, EventArgs e)
        {
            this.Text = "Chat Client - " + mChildConnection.nick;
            // Leave this here.
            txtLog.Text = "Chat Client starting...";
        }

        private void closeChatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
